﻿using CinemaMaestro.DataLayer.Contracts;
using CinemaMaestro.DataLayer.Contracts.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace CinemaMaestro.DataLayer.Entity
{
    public class ActorRepository : IRepository<Actor>
    {
        private DbSet<Actor> _dbSet;
        private DbContext _ctx;

        public ActorRepository(DbContext context)
        {
            _dbSet = context.Set<Actor>();
            _ctx = context;
        }

        public IQueryable<Actor> Read()
        {
            return _dbSet;
        }

        public Actor Create(Actor entity)
        {
            return _dbSet.Add(entity);
        }

        public void Update(Actor entity)
        {
            _dbSet.Attach(entity);
            var entityData = _ctx.Entry(entity);
            entityData.State = EntityState.Modified;
        }

        public bool Delete(int id)
        {
            var instance = _dbSet.
                FirstOrDefault(x => x.Id == id);

            if (instance != null)
            {
                _dbSet.Remove(instance);
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
